package com.horizon.mobile.activity;

import java.util.ArrayList;
import java.util.List;

import zuo.biao.library.base.BaseHttpListActivity;
import zuo.biao.library.interfaces.AdapterCallBack;
import zuo.biao.library.interfaces.OnBottomDragListener;
import zuo.biao.library.manager.HttpManager.OnHttpResponseListener;
import zuo.biao.library.util.JSON;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.horizon.mobile.R;
import com.horizon.mobile.adapter.AgentAdapter;
import com.horizon.mobile.adapter.AgentDetailAdapter;
import com.horizon.mobile.manage.SharePreferences;
import com.horizon.mobile.model.Agent;
import com.horizon.mobile.model.Result;
import com.horizon.mobile.util.HttpRequest;

/**
 * 代办历史记录详情
 * @author 左大明
 *
 */
public class AgentDetailListActivity extends
		BaseHttpListActivity<Agent, AgentDetailAdapter> implements OnBottomDragListener {
	public int page = 1;
	public static final String TITLE = "TITLE";
	public static final String DATAID = "DATAID";
	public List<Agent> listTodo = new ArrayList<Agent>();
	public static final String RESULT_CLICKED_ITEM = "RESULT_CLICKED_ITEM";

	/**
	 * 启动这个Activity的Intent
	 * 
	 * @param context
	 * @return
	 */
	public static Intent createIntent(Context context, String title,
			String dataId) {
		return new Intent(context, AgentDetailListActivity.class).putExtra(TITLE,
				title).putExtra(DATAID, dataId);
	}

	// 启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

	@Override
	public Activity getActivity() {
		return this;
	}

	private String title = "";
	private String dataId = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.agent_detail_list_activity, this);

		intent = getIntent();
		title = intent.getStringExtra(TITLE);
		dataId = intent.getStringExtra(DATAID);

		initView();
		initData();
		initEvent();

		onRefresh();//调用刷新
	}

	// UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

	@Override
	public void initView() {// 必须在onCreate方法内调用
		super.initView();
	}

	@Override
	public void getListAsync(int page) {
//		showProgressDialog(R.string.loading);
		this.page = page;
		HttpRequest.getAgentDetailList(SharePreferences.getInstance(context).getToken(), dataId, page, 10, 0, listener);
	}

	// UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	// Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

	@Override
	public void initData() {// 必须在onCreate方法内调用
		super.initData();
		// 示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

		tvBaseTitle.setText(title);

		// 示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	}

	// Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

	// Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

	@Override
	public void onRefresh() {
		super.onRefresh();
	}

	@Override
	public void initEvent() {// 必须在onCreate方法内调用
		super.initEvent();
		lvBaseList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
//				toActivity(FlowDetailActivity.createIntent(context,
//						listTodo.get(position-1).getWorkId(), listTodo.get(position-1)
//								.getTitle(),listTodo.get(position-1).getTrackId()));
			
			}
		});
		// 示例代码>>>>>>>>>>>>>>>>>>>
	}

	@Override
	public void onDragBottom(boolean rightToLeft) {
		if (rightToLeft) {

			return;
		}

		finish();
	}

	@Override
	public void setList(final List<Agent> list) {
		listTodo = list;
		setList(new AdapterCallBack<AgentDetailAdapter>() {

			@Override
			public void refreshAdapter() {
				adapter.refresh(list);

			}

			@Override
			public AgentDetailAdapter createAdapter() {
				// TODO Auto-generated method stub
				return new AgentDetailAdapter(context);
			}
		});

	}

	OnHttpResponseListener listener = new OnHttpResponseListener() {

		@Override
		public void onHttpResponse(int requestCode, String resultJson,
				Exception e) {
			boolean isNull = resultJson == null;
			if (resultJson != null) {
				Result result = JSON.parseObject(resultJson, Result.class);
				if (result.isSuccess()) {
					List<Agent> list = JSON.parseArray(result.getData()
							.toString(), Agent.class);
					onLoadSucceed(page, list);
					// setList(list);
				} else {
					showShortToast(result.getMessage());
				}
			} else {
//				progressDialog.cancel();
				if (e != null)
					showShortToast(e.getMessage());
				else
					showShortToast("连接服务器失败！");
			}
		}

	};

	@Override
	public List<Agent> parseArray(String json) {
		// TODO Auto-generated method stub
		return JSON.parseArray(json, Agent.class);
	}

}